package cn.mesmile.image.controller;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.TimeInterval;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.ZipUtil;
import cn.mesmile.image.compression.MinioUtils;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.api.client.util.IOUtils;
import com.sun.org.apache.bcel.internal.generic.NEW;
import lombok.Cleanup;
import net.coobird.thumbnailator.Thumbnails;
import org.apache.commons.compress.archivers.sevenz.SevenZArchiveEntry;
import org.apache.commons.compress.archivers.sevenz.SevenZFile;
import org.apache.commons.compress.archivers.sevenz.SevenZOutputFile;
import org.apache.tools.tar.TarEntry;
import org.apache.tools.tar.TarInputStream;
import org.springframework.core.io.Resource;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.net.URI;
import java.util.*;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
import java.util.zip.GZIPInputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

@RequestMapping("/test")
@RestController
public class TestController {

    @PostMapping("/upload")
    public void upload(@RequestParam MultipartFile file) throws InterruptedException, IOException {
        System.out.println("当前开始");
        Thread.sleep(5 * 1000);
        System.out.println("休息5s");
        file.transferTo(new File("D:\\file\\test2"));
    }


    @PostMapping("/gzip")
    public void uploadZip(@RequestParam MultipartFile file) throws IOException, ExecutionException, InterruptedException {
        // 开始计算时间
        TimeInterval timer = DateUtil.timer();

        InputStream inputStream = file.getInputStream();

        String name = file.getName();
        Resource resource = file.getResource();
        byte[] bytes = file.getBytes();
        String contentType = file.getContentType();
        String originalFilename = file.getOriginalFilename();
        long size = file.getSize();

        // file
        System.out.println("name = " + name);
        System.out.println("resource = " + resource);
        System.out.println("resource.exists() = " + resource.exists());
        System.out.println("resource.getDescription() = " + resource.getDescription());
        System.out.println("resource.contentLength() = " + resource.contentLength());
        System.out.println("resource.getFilename() = " + resource.getFilename());
        System.out.println("resource.isReadable() = " + resource.isReadable());
        System.out.println("resource.isFile() = " + resource.isFile());

        System.out.println("bytes = " + bytes);
        // image/jpeg
        System.out.println("contentType = " + contentType);
        // 06S3459.JPG
        System.out.println("originalFilename = " + originalFilename);
        // 13051202
        System.out.println("size = " + size);

//        byte[] byteTest = ZipUtil.unGzip(inputStream, (int) size);

        System.out.println("DateUtil.now() = " + DateUtil.now());

        // ZIP  TAR  7Z  GZ  XZ  ISO  ZIPX  TGZ LGZ
//        TarInputStream tarInputStream = new TarInputStream(inputStream);
//        GZIPInputStream gzipInputStream = new GZIPInputStream(inputStream);
        ExecutorService executorService = Executors.newCachedThreadPool();

        Future<AtomicLong> futureOne = executorService.submit(new Callable<AtomicLong>() {
            @Override
            public AtomicLong call() throws Exception {
                // 解压文件流
                ZipInputStream zis = new ZipInputStream(inputStream);
                ZipEntry zipEntry = null;
                // 循环读取压缩包中的文件
                while ((zipEntry = zis.getNextEntry()) != null) {
                    String names = zipEntry.getName();
                    if (!zipEntry.isDirectory() && StrUtil.containsIgnoreCase(names, "jpg")) {
                        String path = "C:\\Users\\Bing.Zheng1\\Desktop\\images\\";
                        File file2 = new File(path + names);
                        long size1 = zipEntry.getSize();
                        // 上传文件
//                        MinioUtils.upload(names,zis,size1);
                        Thumbnails.of(zis).scale(0.2).outputQuality(1F).toFile(file2);
                    }
                    if (zipEntry.isDirectory()) {
                        while ((zipEntry = zis.getNextEntry()) != null) {
                            names = zipEntry.getName();
                            if (!zipEntry.isDirectory() && StrUtil.containsIgnoreCase(names, "jpg")) {
                                long size1 = zipEntry.getSize();
                                names = StrUtil.subAfter(names, "/", true);
                                // 上传文件
//                                MinioUtils.upload(names,zis,size1);
                                String path = "C:\\Users\\Bing.Zheng1\\Desktop\\images\\";
                                File file2 = new File(path + names);
                                Thumbnails.of(zis).scale(0.2).outputQuality(1F).toFile(file2);
                            }
                        }
                    }
                }
                long currentTimeMillis = System.currentTimeMillis();
                return new AtomicLong(currentTimeMillis);
            }
        });

//        AtomicLong atomicInteger = futureOne.get();
//        long i = atomicInteger.get();
//        System.out.println("atomicInteger.get() = " + i);
//        Date date = new Date(i);
//        System.out.println("DateUtil.formatDateTime(date) = " + DateUtil.formatDateTime(date));


        //花费毫秒数
        long interval = timer.interval();
        //花费分钟数
        long minute = timer.intervalMinute();
        System.out.println("interval = " + interval);
        System.out.println("minute = " + minute);

        long l = timer.intervalRestart();

    }



    @PostMapping("/tar")
    public void uploadTar(@RequestParam MultipartFile file) throws IOException, ExecutionException, InterruptedException {
        // 开始计算时间
        TimeInterval timer = DateUtil.timer();

        InputStream inputStream = file.getInputStream();

        String name = file.getName();
        Resource resource = file.getResource();
        byte[] bytes = file.getBytes();
        String contentType = file.getContentType();
        String originalFilename = file.getOriginalFilename();
        long size = file.getSize();

        // file
        System.out.println("name = " + name);
        System.out.println("resource = " + resource);
        System.out.println("resource.exists() = " + resource.exists());
        System.out.println("resource.getDescription() = " + resource.getDescription());
        System.out.println("resource.contentLength() = " + resource.contentLength());
        System.out.println("resource.getFilename() = " + resource.getFilename());
        System.out.println("resource.isReadable() = " + resource.isReadable());
        System.out.println("resource.isFile() = " + resource.isFile());

        System.out.println("bytes = " + bytes);
        // image/jpeg
        System.out.println("contentType = " + contentType);
        // 06S3459.JPG
        System.out.println("originalFilename = " + originalFilename);
        // 13051202
        System.out.println("size = " + size);

//        byte[] byteTest = ZipUtil.unGzip(inputStream, (int) size);

        System.out.println("DateUtil.now() = " + DateUtil.now());

        // ZIP  TAR  7Z  GZ  XZ  ISO  ZIPX  TGZ LGZ
//        TarInputStream tarInputStream = new TarInputStream(inputStream);
//        GZIPInputStream gzipInputStream = new GZIPInputStream(inputStream);
        ExecutorService executorService = Executors.newCachedThreadPool();

        Future<AtomicLong> futureOne = executorService.submit(new Callable<AtomicLong>() {
            @Override
            public AtomicLong call() throws Exception {
                // 解压文件流
                TarInputStream zis = new TarInputStream(inputStream);
                TarEntry tarEntry = null;
                // 循环读取压缩包中的文件
                while ((tarEntry = zis.getNextEntry()) != null) {
                    String names = tarEntry.getName();
                    if (!tarEntry.isDirectory() && StrUtil.containsIgnoreCase(names, "jpg")) {
                        String path = "C:\\Users\\Bing.Zheng1\\Desktop\\images\\";
                        File file2 = new File(path + names);
                        long size1 = tarEntry.getSize();
                        // 上传文件
//                        MinioUtils.upload(names,zis,size1);
                        Thumbnails.of(zis).scale(0.2).outputQuality(1F).toFile(file2);
                    }
                    if (tarEntry.isDirectory()) {
                        while ((tarEntry = zis.getNextEntry()) != null) {
                            names = tarEntry.getName();
                            if (!tarEntry.isDirectory() && StrUtil.containsIgnoreCase(names, "jpg")) {
                                long size1 = tarEntry.getSize();
                                names = StrUtil.subAfter(names, "/", true);
                                // 上传文件
//                                MinioUtils.upload(names,zis,size1);
                                String path = "C:\\Users\\Bing.Zheng1\\Desktop\\images\\";
                                File file2 = new File(path + names);
                                Thumbnails.of(zis).scale(0.2).outputQuality(1F).toFile(file2);
                            }
                        }
                    }
                }
                long currentTimeMillis = System.currentTimeMillis();
                return new AtomicLong(currentTimeMillis);
            }
        });

//        AtomicLong atomicInteger = futureOne.get();
//        long i = atomicInteger.get();
//        System.out.println("atomicInteger.get() = " + i);
//        Date date = new Date(i);
//        System.out.println("DateUtil.formatDateTime(date) = " + DateUtil.formatDateTime(date));


        //花费毫秒数
        long interval = timer.interval();
        //花费分钟数
        long minute = timer.intervalMinute();
        System.out.println("interval = " + interval);
        System.out.println("minute = " + minute);

        long l = timer.intervalRestart();

    }

}